<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 管理员表格
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="delAllSelection"
                >批量删除</el-button>

                <el-select v-model="query.cinema"  placeholder="影院" class="handle-select mr10" style="width: 200px">
                    <el-option key="" value="" lable=""></el-option>
                    <el-option v-for="c in cinemanameList" :key="c" :value="c" lable="c"></el-option>
                </el-select>

                <el-input v-model="query.name" placeholder="管理员" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>

                <el-button type="primary" icon="el-icon-plus" @click="addManager">增加</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="managerid" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="managername" label="管理员"></el-table-column>
                <el-table-column prop="managerpass"label="密码"></el-table-column>
                <el-table-column prop="cinemaDTO.cinemaname" label="影院" align="center"></el-table-column>
                <el-table-column prop="managerstatus" label="状态" align="center" ><!--:formatter="stateFormat"-->
                    <template slot-scope="scope">
                        <p v-if="scope.row.managerstatus=='0'" style="background-color: #ff5900" >未审核</p>
                        <p v-else-if="scope.row.managerstatus=='1'" style="background-color: #5daf34">审核成功</p>
                        <p v-else-if="scope.row.managerstatus=='2'" style="background-color: red">审核失败</p>
                    </template>

                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageNum"
                        :total="pageTotal"
                        :page-size="query.pageSize"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" @close="closeDialog" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="ID">
                    <el-input v-model="form.managerid" readonly="true"></el-input>
                </el-form-item>
                <el-form-item label="管理员">
                    <el-input v-model="form.managername"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.managerpass"></el-input>
                </el-form-item>
                <el-form-item label="影院">
                    <el-input v-model="form.cinemaname"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-input v-model="form.managerstatus" :readonly="true" style="width: 100px"></el-input>
                    <el-select v-model="form.managerstatus"  class="handle-select mr10">
                        <el-option v-for="s in status" :key="s" :value="s" lable="s"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelEdit">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 增加弹出框 -->
        <el-dialog title="增加" :visible.sync="addVisible" width="30%">
            <el-form ref="addsss" :model="addsss"  label-width="70px">
                <el-form-item label="管理员" prop="managername">
                    <el-input v-model="addsss.managername"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="managerpass">
                    <el-input v-model="addsss.managerpass"></el-input>
                </el-form-item>

                <el-form-item label="影院"  prop="cinemaname">
                <el-select v-model="addsss.cinemaname"  placeholder="影院" class="handle-select mr10" style="width: 100%">
                    <el-option key="" value="" lable=""></el-option>
                    <el-option v-for="n in cinemanameAllList" :key="n.cinemaname" :value="n.cinemaname" lable="n.cinemaname"></el-option>
                </el-select>
                </el-form-item>
                <el-form-item label="状态" prop="managerstatus">
                    <el-select v-model="addsss.managerstatus"  class="handle-select mr10">
                        <el-option v-for="s in status" :key="s" :value="s" lable="s"></el-option>
                    </el-select>
                    <el-input type="hidden" v-model="addsss.managerstatus" :readonly="true" style="width: 100px"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelEdit">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import { fetchData } from '../../api/index';
    export default {
        name: 'Cinema',
        data() {
            return {
                query: {
                    cinema: '',
                    name: '',
                    pageNum: 1,
                    pageSize: 8
                },
                deleteid: '',
                cinemas: {},
                tableData: {},
                pageTotal: 0,
                pages:0,
                status: ['未审核', '审核成功', '审核失败'],
                editVisible: false,
                form: {
                    managerid: '',
                    managername: '',
                    managerpass: '',
                    managerstatus: '',
                    cinemaname: ''
                },
                cinemanameList: {},
                cinemanameAllList: {},
                addsss: {
                    managername: '',
                    managerpass: '',
                    cinemaname: '',
                    managerstatus: ''
                },
                addVisible: false,
                multipleSelection: [],
                /*delList: [],*/
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            getData() {
                this.axios.get("api/super/list",{
                      params: {
                        cinema: this.query.cinema,
                        name: this.query.name,
                        pageNum: this.query.pageNum,
                        pageSize: this.query.pageSize
                      }
                    }
                ).then((res) => {
                    console.log(res)
                    /*console.log(this.query);
                    console.log(res.pageInfo.list);
                    console.log(res.pageInfo.total);*/
                    this.tableData = res.data.pageInfo.list;
                    this.pageTotal = res.data.pageInfo.total;
                    this.pages=res.data.pageInfo.pages;
                    this.cinemanameList = res.data.cinemanameList;
                    this.cinemanameAllList = res.data.cinemaList;
                });
            },
            // 分页
            handlePageChange(val) {
                this.$set(this.query, 'pageNum', val);
                this.getData();
            },
            //查询
            handleSearch() {
                this.getData();
            },

            // 删除操作
            handleDelete(index, row) {
                //二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                },).then(() => {
                    this.axios.get("api/super/delete", {
                        params: {
                            managerid: row.managerid
                        }
                    }).then(res => {
                        /*console.log(res)*/
                        if (res.data) {
                            this.getData();
                            this.$message.success('删除成功!');
                        } else {
                            this.$message.error('删除失败!');
                        }
                    })
                }).catch(() => {
                    this.$message.info('已取消删除!');
                });

            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 多选删除操作
            delAllSelection() {
                this.$confirm('确定要删除吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                },).then(() => {
                var arrids= this.multipleSelection.map(item => item.managerid).join();
                console.log(arrids);
                this.axios.post("api/super/deleteAll", {
                    arrids: arrids
                }).then(res => {
                    if (res.data) {
                        this.$message.success(`删除成功`);
                        this.getData();
                    } else {
                        this.$message.error(`删除失败`);
                    }
                })
                    }).catch(() => {
                    this.$message.info('已取消删除!');
                });
            },
            // 编辑操作
            handleEdit(index, row) {
                /* console.log(index);*/
                console.log(row);
                this.form = row;
                this.form.managerstatus = row.managerstatus == 1 ? '审核成功' : (row.managerstatus == 2 ? '审核失败' : '未审核');
                this.form.cinemaname = row.cinemaDTO.cinemaname;
                this.editVisible = true;
            },
            //取消编辑
            cancelEdit() {
                this.getData();
                this.editVisible = false;
                this.addVisible = false;
            },
            // 保存编辑
            saveEdit() {
                this.$set(this.tableData, this.idx, this.form);
                this.axios.get("api/super/update", {
                    params: {
                        managerid: this.form.managerid,
                        managername: this.form.managername,
                        managerpass: this.form.managerpass,
                        cinemaname: this.form.cinemaname,
                        managerstatus: this.form.managerstatus
                    }
                }).then(res => {
                    if (res.data) {
                        this.getData();
                        this.$message.success(`修改成功!`);
                    } else {
                        this.$message.error(`修改失败!`);
                    }
                });
                this.editVisible = false;
            },
            //关闭编辑模态框
            closeDialog(){
                this.getData();
            },

            //增加操作
            addManager() {

                this.addVisible = true;
                //清空表单数据
                this.$refs['addsss'].resetFields();

            },
            //保存添加
            saveAdd() {
                this.axios.get("api/super/add", {
                    params: {
                        managername: this.addsss.managername,
                        managerpass: this.addsss.managerpass,
                        cinemaname: this.addsss.cinemaname,
                        managerstatus: this.addsss.managerstatus
                    }
                }).then(res => {
                    if (res.data) {
                        this.$message.success(`添加成功`);
                        this.getData();
                    } else {
                        this.$message.error(`添加失败`);
                    }

                });
                this.addVisible = false;
            }

        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
